﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

using BusinessManagementStudio.Models.BusinessObject.Entities;
using MongoDB.Driver.Builders;
using MongoDB.Driver.Linq;

namespace BusinessManagementStudio.Models.BusinessObject.Services
{
    public class InvoiceService : EntityService<Invoice>
    {
        public List<Invoice> GetAllInvoices()
        {
            return (from invoice in this.MongoConnectionHandler.MongoCollection.FindAllAs<Invoice>()
                    orderby invoice.InvoiceDate, invoice.InvoiceNo
                    select invoice).ToList();
        }

        public List<Invoice> GetAllInvoices_WORpt()
        {
            return (from invoice in this.MongoConnectionHandler.MongoCollection.FindAllAs<Invoice>()
                    where invoice.WorkOrderNo != "" && invoice.WorkOrderNo != null
                    orderby invoice.InvoiceDate, invoice.InvoiceNo
                    select invoice).ToList();
        }

        public Invoice GetJLLPOInvoice_ByQuotationNo(String QuotationNo)
        {
            return (from invoice in this.MongoConnectionHandler.MongoCollection.FindAllAs<Invoice>()
                    where invoice.QuotationNo == QuotationNo
                    orderby invoice.InvoiceNo
                    select invoice).FirstOrDefault();
        }

        public override void Update(Invoice entity)
        {
            this.MongoConnectionHandler.MongoCollection.Update(
                    Query.And(Query<Invoice>.EQ(o => o.QuotationNo, entity.QuotationNo),
                    Query<Invoice>.EQ(o => o.InvoiceNo, entity.InvoiceNo)),
                    Update<Invoice>.Set(o => o.ReceivedDate, entity.ReceivedDate)
                    .Set(o => o.IsReceived, entity.IsReceived)
                    .Set(o => o.ReceivedRef, entity.ReceivedRef));
        }
    }
}